package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin //允许跨域
@Slf4j
@RequestMapping("/courses")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 分页查询
     */
    @GetMapping
    public Result list(String name,
                       Integer subject,
                       Integer target,
                       @RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询课程:{},{},{},{},{}",name,subject,target,page,pageSize);
        PageResult<Course> pageResult =courseService.list(name,subject,target,page,pageSize);
        return Result.success(pageResult);
    }
    /**
     * 删除数据
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除课程id:{}", id);
        courseService.deleteById(id);
        return Result.success();
    }

    /**
     * 添加数据
     */
    @PostMapping
    public Result add(@RequestBody Course  course){
        log.info("添加课程:{}",course);
        courseService.add(course);
        return Result.success();
    }
    /**
     * 根据id查询数据
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询课程id:{}", id);
        Course course = courseService.selectById(id);
        return Result.success(course);
    }
    /**
     * 修改数据
     */
    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("修改课程:{}", course);
        courseService.update(course);
        return Result.success();
    }
    /**
     * 查询所有课程
     */
    @GetMapping("/list")
    public Result listAll(){
        List<Course> coursesList =courseService.select();
        return Result.success(coursesList);
    }
    /**
     * 根据学科查询课程
     */
    @GetMapping("/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject){
        List<Course> coursesList =courseService.selectBySubject(subject);
        return Result.success(coursesList);
    }


}
